package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.base.Code;
import me.zhengjie.enums.DeletedEnum;
import me.zhengjie.modules.system.converter.MachineConverter;
import me.zhengjie.modules.system.domain.entity.SysUserMerchant;
import me.zhengjie.modules.system.domain.req.SysUserMerchantReqDTO;
import me.zhengjie.modules.system.domain.req.SysUserMerchantReqQueryDTO;
import me.zhengjie.modules.system.domain.rsp.SysUserMerchantRspDTO;
import me.zhengjie.modules.system.mapper.SysUserMerchantMapper;
import me.zhengjie.modules.system.service.SysUserMerchantService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Transactional;


@Service
@RequiredArgsConstructor
public class SysUserMerchantServiceImpl extends ServiceImpl<SysUserMerchantMapper, SysUserMerchant> implements
        SysUserMerchantService {


    /**
     * 添加
     * @param reqDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean save(SysUserMerchantReqDTO reqDTO) {
        SysUserMerchant sysUserMerchant = MachineConverter.INSTANCE.sysUserMerchantReqDTOTo(reqDTO);
        return super.save( sysUserMerchant);
    }

    /**
     * 修改
     * @param reqDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(SysUserMerchantReqDTO reqDTO) {
        SysUserMerchant sysUserMerchant =MachineConverter.INSTANCE.sysUserMerchantReqDTOTo(reqDTO);
        return super.updateById(sysUserMerchant);
    }


    /**
     * 删除
     * @param code
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Code code) {
        return super.removeById(code.getCode());
    }

    /**
     * 主键查询
     * @param code
     */
    public SysUserMerchantRspDTO findById(Code code) {
        SysUserMerchant sysUserMerchant =lambdaQuery().eq(SysUserMerchant::getId, code.getCode())
                .eq(SysUserMerchant::getIsDel, DeletedEnum.YES.getCode())
                .one();
        return MachineConverter.INSTANCE.sysUserMerchantToRsp(sysUserMerchant);
    }

    /**
     * 列表
     * @param reqQueryDTO
     * @return
     */
    public PageResult<SysUserMerchantRspDTO> findAll(SysUserMerchantReqQueryDTO reqQueryDTO) {
        PageHelper.startPage(reqQueryDTO.getPageNo(), reqQueryDTO.getPageSize(), true);
        List<SysUserMerchantRspDTO> list = getBaseMapper().findAll(reqQueryDTO);
        PageInfo<SysUserMerchantRspDTO> pageInfo = new PageInfo<>(list);
        return PageUtil.toPage(pageInfo.getList(), pageInfo.getTotal());
    }

}
